package com.wsq.aidlservice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.wsq.aidlservice.aidl.AIDLService;
import com.wsq.aidlservice.aidl.IServiceAidlInterface;

import java.util.concurrent.ThreadPoolExecutor;

public class MainActivity extends AppCompatActivity {

    AIDLService aidlService ;
    IServiceAidlInterface iServiceAidlInterface;

    ThreadPoolExecutor executor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e("service", "MainActivity 启动了");

        Handler handler;

        Intent intent = new Intent(this, AIDLService.class);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            startForegroundService(intent);
//        }
        bindService(intent, connection, Context.BIND_AUTO_CREATE);

    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("service", "MainActivity java 线程id：" +android.os.Process.myTid());
            Log.e("AIDL", "MainActivity 连接上了");
            Log.e("service", "MainActivity Thread: " + Thread.currentThread().getName());
            iServiceAidlInterface = IServiceAidlInterface.Stub.asInterface(service);
            try {
                iServiceAidlInterface.basicTypes(1,2,true,1.0f, 1.111, "吃鸡");
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("AIDL", "MainActivity 断开了");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbindService(connection);
    }
}
